package org.dromara.system.controller.system;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.*;
import org.dromara.system.domain.vo.SimCardStatusCountVo;
import org.dromara.system.domain.vo.SimCardVo;
import org.dromara.system.listener.SimCardListener;
import org.dromara.system.service.ISimCardService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 流量卡
 *
 * @author Lion Li
 * @date 2025-08-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/card")
public class SimCardController extends BaseController {

    private final ISimCardService simCardService;

    /**
     * 查询流量卡列表
     */
//    @SaCheckPermission("system:card:list")
    @GetMapping("/list")
    public TableDataInfo<SimCardVo> list(SimCardBo bo, PageQuery pageQuery) {
        return simCardService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/listReception")
    public TableDataInfo<SimCardVo> listReception(SimCardBo bo, PageQuery pageQuery) {
        return simCardService.listReception(bo, pageQuery);
    }

    @GetMapping("/querySimCardDistinguishStatus")
    public R<SimCardStatusCountVo> querySimCardDistinguishStatus(SimCardBo bo) {
        return R.ok(simCardService.querySimCardDistinguishStatus(bo));
    }


    /**
     * 获取流量卡详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:card:query")
    @GetMapping("/{id}")
    public R<SimCardVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long id) {
        return R.ok(simCardService.queryById(id));
    }

    /**
     * 新增流量卡
     */
//    @SaCheckPermission("system:card:add")
    @Log(title = "流量卡", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SimCardBo bo) {
        return toAjax(simCardService.insertByBo(bo));
    }

    /**
     * 修改流量卡
     */
//    @SaCheckPermission("system:card:edit")
    @Log(title = "流量卡", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SimCardBo bo) {
        return toAjax(simCardService.updateByBo(bo));
    }

    /**
     * 删除流量卡
     *
     * @param ids 主键串
     */
    @Log(title = "流量卡", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(simCardService.deleteWithValidByIds(List.of(ids), true));
    }

    //-----------------------------------------------iccId入库-------------------------------------------------------------------------------------------
    //iccId文件入库
    @PostMapping(value = "/importSimCardByFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importSimCard(@RequestPart("file") MultipartFile file) throws Exception {
        // 处理解析结果
        ImportSimCardBo importSimCardBo = ImportSimCardBo.builder()
            .isSupportUpdate(Boolean.TRUE)
            .build();
        ExcelResult<SimCardVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), SimCardVo.class, new SimCardListener(importSimCardBo));
        return R.ok(excelResult.getAnalysis());
    }

    @PostMapping(value = "/importSimCardByIccId", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit()
    public R<Void> importMachineByIccId(@RequestParam(value = "iccId", required = true) String iccId,
                                        @RequestParam(value = "company", required = false) String company,
                                        @RequestParam(value = "sn", required = false) String sn) {
        SimCardBo simCardBo = SimCardBo.builder().iccId(iccId).company(company).sn(sn).build();
        return toAjax(simCardService.importMachineByIccId(simCardBo));
    }

    //-----------------------------------------------iccId分配-------------------------------------------------------------------------------------------
    //多选iccId分配
    @PostMapping(value = "/allocateSimCardByIccId")
    @RepeatSubmit()
    public R<Void> allocateSimCardByIccId(@RequestBody AllocateSimCardByIccIdBo bo) {
        return toAjax(simCardService.allocateSimCardByIccId(bo));
    }

    //文件分配
    @PostMapping(value = "/allocateSimCardByFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit()
    public R<Void> allocateSimCardByFile(@RequestPart("file") MultipartFile file
        , @RequestParam(value = "currentUserId", required = true) Long currentUserId
        , @RequestParam(value = "reNewNum", required = true) Integer reNewNum) {
        return toAjax(simCardService.allocateSimCardByFile(file, currentUserId, reNewNum));
    }

    @PostMapping(value = "/freshenIccId/{iccId}")
    public R<Void> freshenIccId(@NotNull(message = "iccId不能为空") @PathVariable String iccId) {
        return toAjax(simCardService.queryIccIdInfo(iccId));
    }

    //批量刷新sim
    @PostMapping(value = "/freshenIccId")
    public R<Void> batchFreshen(@RequestBody FreshenIccIdBo bo) {
        return toAjax(simCardService.batchFreshen(bo));
    }

    //停复机
    @PostMapping(value = "/stopOrRestartSimCard")
    public R<Void> stopOrRestartSimCard(@RequestBody StopOrRestartSimCardBo bo) {
        return toAjax(simCardService.stopOrRestartSimCard(bo));
    }


}
